package playground;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.lang.reflect.Method;
import java.util.Arrays;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import MediaPlayer.MPlayer;
import MediaPlayer.Player;

public class MPlayerTesting {
	private Player p;
	private JTextField text;
	
	public MPlayerTesting() {
		text = new JTextField(30);
		text.setText("D:\\Bleach - 55.avi");
		p = new MPlayer();
		JFrame f = new JFrame("Test");
		JPanel p = new JPanel(new FlowLayout());		
		p.add(text);
		
		Class<? extends Player> c = this.p.getClass();
		
		for (Method m : c.getMethods()) {
			p.add(new JButton(new ReflectAct(m)));
		}
		f.setContentPane(p);
		f.pack();
		f.setVisible(true);
	}
	
	
	public void test(String media) throws Exception{
	
	}
	
	
	public void pause() throws Exception{

	}
	
	public static void main(String[] args) throws Exception{
		new MPlayerTesting();
	}
	
	@SuppressWarnings("serial")
	class ReflectAct extends AbstractAction {
		Method m;
		
		ReflectAct(Method m) {
			super(m.getName() + (m.getParameterTypes().length == 0 ? "" : Arrays.toString(m.getParameterTypes())));
			this.m = m;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				if (m.getReturnType() != void.class) {
					if (m.getParameterTypes().length == 0) {
						System.out.println(m.invoke(p));
					} else if (m.getParameterTypes().length == 1) {
						if (m.getParameterTypes()[0].equals(int.class)) {
							System.out.println(m.invoke(p, Integer.parseInt(text.getText())));
						} else if (m.getParameterTypes()[0].equals(double.class)) {
							System.out.println(m.invoke(p, Double.parseDouble(text.getText())));
						} else if (m.getParameterTypes()[0].equals(String.class)) {
							System.out.println(m.invoke(p, text.getText()));
						}
					}
				} else {
					if (m.getParameterTypes().length == 0) {
						m.invoke(p);
					} else if (m.getParameterTypes().length == 1) {
						if (m.getParameterTypes()[0].equals(int.class)) {
							m.invoke(p, Integer.parseInt(text.getText()));
						} else if (m.getParameterTypes()[0].equals(double.class)) {
							m.invoke(p, Double.parseDouble(text.getText()));
						} else if (m.getParameterTypes()[0].equals(String.class)) {
							m.invoke(p, text.getText());
						}
					}
				}
			}catch (Exception e) {}
		}
	}
}
